package br.ebula.apl.controladores;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.ebula.apl.bean.Filtro;
import br.ebula.apl.dto.DTOLogin;
import br.ebula.apl.fachada.FachadaRealizaLogin;

public class ServletRealizaLogin extends HttpServlet {

	private static final long serialVersionUID = 8803728404258649818L;

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		doGet(req, resp);
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) {

		DTOLogin dto = new DTOLogin();
		dto.retiraLogin(req);

		FachadaRealizaLogin fachada = new FachadaRealizaLogin();
		fachada.realizaLogin(dto);

		try {
			if(dto.isUsuarioLogado()){
				Filtro filtro = new Filtro(new Character('A'));
				req.setAttribute("filtro", filtro);
				
				ServletListaBulas s = new ServletListaBulas();
				s.doPost(req, resp);
			}
			else{
				req.setAttribute("realiza_login", dto);
				RequestDispatcher r = req.getRequestDispatcher("apresentacao/realiza_login.jsp");
				r.forward(req, resp);
			}
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
